<template>
  <div class="login">
    <div class="login-content">
      <div class="login-form">
        <el-form ref="registerFormRef" :model="registerData" :rules="registerRules">
          <div class="form-title">
            <h2>{{ $t("register.title") }}</h2>
          </div>
          <el-form-item prop="customerName">
            <div class="input-wrapper">
              <el-icon class="mx-2">
                <User />
              </el-icon>
              <el-input
                ref="customerName"
                v-model="registerData.customerName"
                :placeholder="$t('register.customerName')"
                name="customerName"
                size="large"
                class="h-[48px]"
              />
            </div>
          </el-form-item>
          <el-form-item prop="customerEmail">
            <div class="input-wrapper">
              <el-icon class="mx-2">
                <Message />
              </el-icon>
              <el-input
                ref="customerEmail"
                v-model="registerData.customerEmail"
                :placeholder="$t('register.customerEmail')"
                name="customerEmail"
                size="large"
                class="h-[48px]"
              />
            </div>
          </el-form-item>
          <el-form-item prop="customerPassword">
            <div class="input-wrapper">
              <el-icon class="mx-2">
                <Lock />
              </el-icon>
              <el-input
                ref="customerPassword"
                v-model="registerData.customerPassword"
                :placeholder="$t('register.customerPassword')"
                type="password"
                name="customerPassword"
                size="large"
                class="h-[48px] pr-2"
                show-password
              />
            </div>
          </el-form-item>

          <el-button
            :loading="loading"
            type="primary"
            size="large"
            class="w-full"
            @click.prevent="handleRegisterSubmit"
          >
            {{ $t("register.submit") }}
          </el-button>
          <router-link :to="{ name: 'Login' }">
            <p style="text-align: center">
              <e-text>{{ $t("register.haveAccount") }},</e-text>
              <el-link type="primary">{{ $t("register.gotoLogin") }}</el-link>
            </p>
          </router-link>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useI18n } from "vue-i18n";
import { useRouter } from "vue-router";
import { ref } from "vue";
import type { FormInstance } from "element-plus";
import CustomerAPI, { Form } from "@/api/customer";

const { t } = useI18n();
const router = useRouter();

const registerFormRef = ref<FormInstance>();
const loading = ref(false);

const registerData = reactive<Form>({
  customerName: "",
  customerEmail: "",
  customerPassword: "",
  customerType: 2,
  customerId: 0,
});

const registerRules = computed(() => {
  return {
    customerName: [
      {
        required: true,
        message: t("register.rules.name.required"),
        trigger: "blur",
      },
    ],
    customerEmail: [
      {
        required: true,
        message: t("register.rules.email.required"),
        trigger: "blur",
      },
    ],
    customerPassword: [
      {
        required: true,
        message: t("register.rules.password.required"),
        trigger: "blur",
      },
      {
        min: 6,
        message: t("profile.passwdLength"),
        trigger: "blur",
      },
    ],
    customerType: [
      {
        required: true,
        message: t("register.rules.type.required"),
        trigger: "change",
      },
    ],
  };
});
// submit to register
async function handleRegisterSubmit() {
  registerFormRef.value?.validate((valid: boolean) => {
    if (valid) {
      loading.value = true;
      CustomerAPI.doRegister(registerData)
        .then((res) => {
          if (res != null) {
            if (res.customerId > 0) {
              ElMessage.success(t("register.customerSuccess"));
              loading.value = false;
              router.push({
                name: "CustomerRegisterActive",
                params: { email: btoa(registerData.customerEmail) },
              });
            } else {
              ElMessage.error(t("register.registerFailed"));
            }
          } else {
            ElMessage.error(t("register.registerFailed"));
          }
        })
        .finally(() => (loading.value = false));
    }
  });
}
</script>

<style lang="scss" scoped>
.login {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  overflow-y: auto;
  background: url("@/assets/images/login-background-light.jpg") no-repeat center right;

  .login-header {
    position: absolute;
    top: 0;
    display: flex;
    justify-content: right;
    width: 100%;
    padding: 15px;

    .logo {
      width: 26px;
      height: 26px;
    }

    .title {
      margin: auto 5px;
      font-size: 24px;
      font-weight: bold;
      color: #3b82f6;
    }
  }

  .login-content {
    display: flex;
    width: 450px;
    overflow: hidden;
    background-color: #fff;
    border-radius: 5px;
    box-shadow: var(--el-box-shadow-light);

    @media (width <= 768px) {
      flex-direction: column;
      max-width: 100%;
      height: 100vh;
      padding: 0 30px;
      border-radius: 0;
      box-shadow: none;
    }

    .login-img {
      display: flex;
      flex: 3;
      align-items: center;
      justify-content: center;
      background: linear-gradient(60deg, #165dff, #6aa1ff);

      @media (width <= 768px) {
        display: none;
      }
    }

    .login-form {
      display: flex;
      flex: 2;
      flex-direction: column;
      justify-content: center;
      min-width: 400px;
      padding: 30px;

      @media (width <= 768px) {
        width: 100%;
        padding: 0 20px;
      }

      .form-title {
        position: relative;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 0 0 20px;
        text-align: center;
      }

      .input-wrapper {
        display: flex;
        align-items: center;
        width: 100%;
      }

      .captcha-img {
        height: 48px;
        cursor: pointer;
        border-top-right-radius: 6px;
        border-bottom-right-radius: 6px;
      }

      .third-party-login {
        display: flex;
        justify-content: center;
        width: 100%;
        color: var(--el-text-color-secondary);

        *:not(:first-child) {
          margin-left: 20px;
        }

        .icon {
          cursor: pointer;
        }
      }
    }
  }

  .login-footer {
    position: absolute;
    bottom: 0;
    width: 100%;
    text-align: center;
  }
}

:deep(.el-form-item) {
  background: var(--el-input-bg-color);
  border: 1px solid var(--el-border-color);
  border-radius: 5px;
}

:deep(.el-input) {
  .el-input__wrapper {
    padding: 0;
    background-color: transparent;
    box-shadow: none;

    &.is-focus,
    &:hover {
      box-shadow: none !important;
    }

    input:-webkit-autofill {
      transition: background-color 1000s ease-in-out 0s;
    }
  }
}
</style>
